from ferris import Controller, scaffold
from ferris.components.pagination import Pagination


class Artists(Controller):
    class Meta:
        components = (scaffold.Scaffolding, Pagination)
        pagination_limit = 25

    def list(self):
        if 'query' in self.request.params:
            query = self.request.params['query']
            self.context['search_query'] = query
        else:
            query = None

        self.context['artists'] = self.meta.Model.list_query(query)

    def view(self, key):
        item = self.util.decode_key(key).get()
        if not item:
            return 404

        self.context['artist'] = item
        self.context['related_name'] = 'features'
        self.context['related'] = Pagination(self).paginate(item.all_features())

    add = scaffold.add
    edit = scaffold.edit
    delete = scaffold.delete
